﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;


namespace LedController
{
    [Serializable]
    public partial class LedArray : FlowLayoutPanel
    {
        public Dictionary<string, LedBulb> Leds { get; private set; }
        
        public string ArrayName { get; set; }
        
        private int _elements;
        public int Elements
        {
            get { return _elements; }
            set
            {
                _elements = value;
                Init();
            }
        }
        
        private int _ledSize;
        public int LedSize
        {
            get { return _ledSize; }
            set
            {
                _ledSize = value;
                foreach (var led in Leds.Values)
                {
                    led.Height = _ledSize;
                    led.Width = _ledSize;
                }
            }
        }

        private int _ledPadding;
        public int LedPadding
        {
            get { return _ledPadding; }
            set
            {
                _ledPadding = value;
                foreach (var led in Leds.Values)
                {
                    led.Padding=new Padding(LedPadding);
                }
            }
        }

        private Color _ledColor;
        public Color LedColour
        {
            get { return _ledColor; }
            set
            {
                _ledColor = value;
                foreach (var led in Leds.Values)
                {
                    led.Color = _ledColor;
                }
            }
        }

        public LedArray(string name, IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            Leds = new Dictionary<string, LedBulb>();
            ArrayName = name;
            Elements = 7;
            LedSize = 90;
            LedColour = Color.Crimson;
            Init();
        }

        public LedArray(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
            Leds = new Dictionary<string, LedBulb>();

            Init();
        }

        private void Init()
        {
            this.Controls.Clear();
            Leds.Clear();

            for (int i = 1; i <= Elements; i++)
                CreateLed(i);

            this.BackColor = Color.Transparent;
            this.AutoSize = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.WrapContents = false;
        }

        private void CreateLed(int i)
        {
            var led = new LedBulb();
            led.On = true;
            led.Name = ArrayName + i.ToString();
            led.Color = LedColour;
            led.On = false;
            led.Padding = new Padding(LedPadding);
            led.Size = new System.Drawing.Size(LedSize, LedSize);
            led.TabIndex = i;

            //this.ledBulb5.Click += new System.EventHandler(this.ledBulb_Click);
            Leds.Add(led.Name, led);
            this.Controls.Add(led);
        }
    }
}
